import java.io.*;
import java.util.*;

//BioJava libraries
import org.biojava.bio.*;
import org.biojava.bio.seq.*;
//BioJava extension libraries
import org.biojavax.*;
import org.biojavax.bio.seq.*;

public class GenBankParser {
	// Create the RichSequence object
	RichSequence richSeq;
	LinkedList<Integer []> indexGeneList;

	// ExtractInformation constructor
	public GenBankParser(File dnaFile) {
		// Load the sequence file
		try {
			indexGeneList = new LinkedList<Integer []>();
			richSeq = RichSequence.IOTools.readGenbankDNA(
					new BufferedReader(new FileReader(dnaFile)), null)
					.nextRichSequence();
		} catch (FileNotFoundException fnfe) {
			System.out.println("FileNotFoundException: " + fnfe);
		} catch (BioException bioe1) {
			MainInterface.mostrarAviso("Error", "Not a Genbank sequence");
			System.exit(0);
		}
		// Filter the sequence on gene features
		FeatureFilter ff = new FeatureFilter.ByType("gene");
		FeatureHolder fh = richSeq.filter(ff);

		// Iterate through the gene features
		for (Iterator<Feature> i = fh.features(); i.hasNext();) {
			RichFeature rf = (RichFeature) i.next();

			// Get the location of the feature
			String featureLocation = rf.getLocation().toString();

			// Get the annotation of the feature
			RichAnnotation ra = (RichAnnotation) rf.getAnnotation();

			// Iterate through the notes in the annotation
			for (Iterator<Note> it = ra.getNoteSet().iterator(); it.hasNext();) {
				Note note = it.next();
			}

			// Output the feature information
			Integer [] index = new Integer [2];
			String [] temp = new String [2];
			temp = featureLocation.toString().split("\\.\\.");
			// Filter out the ".." between start and end gene position
			try {
				index[0] = Integer.parseInt(temp[0]);
				index[1] = Integer.parseInt(temp[1]);
			} catch (Exception e) {
				MainInterface.mostrarAviso("ERROR", "The GenBank File is not in the correct format. Check this file and try again.");
				System.exit(0);
			}
			
			indexGeneList.add(index);
			// Pass parsed values into index for use by queryContained method
		}
	}

}